package com.edu.hre.struts.action.pim;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreEmpLicense;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpLicenseUpdateForm;

public class EmpLicenseUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(EmpLicenseUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "empLicenseUpdate";

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		CommonUtils.println(CommonUtils.getSourceCodeLocation());

		EmpLicenseUpdateForm beanForm = (EmpLicenseUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			
			int empNum = CommonUtils.getIntParameter("emp_num", request);
			beanForm.setEmpNumber(empNum);
			
			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		Date startDate = CommonUtils.stringToDate(CommonUtils.normalizeString(beanForm.getIssuedDate()));
		Date endDate = CommonUtils.stringToDate(CommonUtils.normalizeString(beanForm.getExpiryDate()));

		HreEmpLicense tmp = new HreEmpLicense();
		tmp.setEmpNumber(beanForm.getEmpNumber());
		tmp.setLicenseId(beanForm.getLicenseType());
		tmp.setLicenseNo(CommonUtils.normalizeString(beanForm.getLicenseNo()));
		tmp.setLicenseIssuedDate(startDate);
		tmp.setLicenseExpiryDate(endDate);
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		EmpLicenseUpdateForm beanForm = (EmpLicenseUpdateForm) form;
		beanForm.setIsAddNew(false);
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){
			
			int id = CommonUtils.getIntParameter("id", request);
			HreEmpLicense tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) id, null, null, 0, HreEmpLicense.class);
			
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				initFormBean(beanForm);
				return mapping.findForward(ACTION_NAME);
			}

			beanForm.setId(id);
			beanForm.setEmpNumber(tmp.getEmpNumber());
			beanForm.setLicenseType(tmp.getLicenseId());
			beanForm.setLicenseNo(CommonUtils.normalizeString(tmp.getLicenseNo()));
			beanForm.setIssuedDate(CommonUtils.dateToString(tmp.getLicenseIssuedDate()));
			beanForm.setExpiryDate(CommonUtils.dateToString(tmp.getLicenseExpiryDate()));

			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);

		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		Date startDate = CommonUtils.stringToDate(beanForm.getIssuedDate());
		Date endDate = CommonUtils.stringToDate(beanForm.getExpiryDate());

		HreEmpLicense tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) beanForm.getId(), null, null, 0, HreEmpLicense.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setLicenseId(beanForm.getLicenseType());
		tmp.setLicenseNo(CommonUtils.normalizeString(beanForm.getLicenseNo()));
		tmp.setLicenseIssuedDate(startDate);
		tmp.setLicenseExpiryDate(endDate);
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(
				 "<script language=\"javascript\">\n" +
						" parent.dw.close();" + 
						 " var param = parent.document.createElement('input'); " +
						 " param.type = 'hidden'; " +
						 " param.name = 'id'; " +
						 " param.value = " + tmp.getEmpNumber() +  ";" +
						 " parent.document.forms[0].appendChild(param);" +
						" parent.document.forms[0].submit(); \n" +
				 "</script>");
		return null;
	}

	private void initFormBean (EmpLicenseUpdateForm beanForm) {

		beanForm.setAllLicenses(ComboBoxInit.getAllLicenses(false));
	}
	
	private String validateForm (EmpLicenseUpdateForm beanForm) {

		Date startDate = CommonUtils.stringToDate(CommonUtils.normalizeString(beanForm.getIssuedDate()));
		Date endDate = CommonUtils.stringToDate(CommonUtils.normalizeString(beanForm.getExpiryDate()));
		
		if (endDate != null) {
			
			if (startDate == null) {
				return StringConstants.BLANK;
			}

			if (!endDate.after(startDate)) {
				return "Expiry date must be after issued date!";
			}
		}
		
		if (CommonUtils.checkLaterThanToday(beanForm.getIssuedDate(), -1, -1, -1)) {
			return "Issued date must not be in the future!";
		}

		return StringConstants.BLANK;
	}
}
